﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using D2L.Extensibility.AuthSdk;
using D2L.Extensibility.AuthSdk.Restsharp;

namespace Teste.PoC.Valence.CSharp
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>



    public class Authenticate
    {

        #region Environment Attributes

        protected const string APP_ID = "G9nUpvbZQyiPrk3um2YAkQ";
        protected const string APP_KEY = "ybZu7fm_JKJTFwKEHfoZ7Q";

        protected const string USER_ID = "KOOxZJijDZ3k7cnre-fHFK";
        protected const string USER_KEY = "bCdB88XbvWwpeL6JAQVWNF";

        protected const int LMS_PORT = 443;
        protected const string LMS_HOST = "lms.valence.desire2learn.com";
        protected const string LMS_HOST_HTTPS = "https://lms.valence.desire2learn.com";

        public const string LP_VERSION = "1.2";





        #endregion

        #region Common Objects

        private D2LAppContextFactory _appFactory = new D2LAppContextFactory();
        private ID2LAppContext _appContext;


        #endregion

        public Authenticate() //class constructor
        {
            _appContext = _appFactory.Create(Authenticate.APP_ID, Authenticate.APP_KEY);
        }

        public RestSharp.IRestResponse ExecuteService_with_UserContext(string serviceToCall, RestSharp.Method RestMethod)
        {
            ID2LUserContext _appUserCtx = _appContext.CreateUserContext(CreateOrRetrieveUserContextProperties(serviceToCall));
            ValenceAuthenticator _vAuth = new ValenceAuthenticator(_appUserCtx);
            RestSharp.RestClient _rClient = new RestSharp.RestClient(Authenticate.LMS_HOST_HTTPS);
            RestSharp.RestRequest _rRequest = new RestSharp.RestRequest(serviceToCall, RestMethod);

            _vAuth.Authenticate(_rClient, _rRequest);

            return _rClient.Execute(_rRequest);
        }

        public RestSharp.IRestResponse ExecuteService_with_AnonymousUserContext(string serviceToCall, RestSharp.Method RestMethod)
        {

            HostSpec _hs = new HostSpec(serviceToCall, Authenticate.LMS_HOST_HTTPS, Authenticate.LMS_PORT);
            ID2LUserContext _appUserCtx = _appContext.CreateAnonymousUserContext(_hs);
            ValenceAuthenticator _vAuth = new ValenceAuthenticator(_appUserCtx);
            RestSharp.RestClient _rClient = new RestSharp.RestClient(Authenticate.LMS_HOST_HTTPS);
            RestSharp.RestRequest _rRequest = new RestSharp.RestRequest(serviceToCall, RestMethod);

            _vAuth.Authenticate(_rClient, _rRequest);

            return _rClient.Execute(_rRequest);

        }

        #region Helper Methods

        private UserContextProperties CreateOrRetrieveUserContextProperties(string serviceToCall)
        {

            return (new UserContextProperties()
            {
                HostName = Authenticate.LMS_HOST,
                Port = Authenticate.LMS_PORT,
                Scheme = serviceToCall,
                UserId = Authenticate.USER_ID,
                UserKey = Authenticate.USER_KEY
            });

            //TODO: reuse a saved used properties

        }

        #endregion

    }


  
}
